  function [sigma,d1,v1]=drawsigma(erdata,v0,s0); 
% function [sigma,d1,v1]=drawsigma(erdata,v0,s0); 
% 
% Draws from the posterior of a  Variance 
% distributed as in IG(v0,s0)
% When the prior for B is independent of sigma 
%
% Formulas follow Kim and Nelson (1998)
%
% v0 is the degrees of freedom   
% s0 is the scale parameter 
% Posterior is: 
% sigma^2 ~ IG ([T+ v0 , s0+erdata'erdata )
%
% sigma     sampled variance 
% d1        posterior degrees of freedom 
% v1        posterior scale parameter 
%
% Using integer values for v0 results in faster sampling 
%
% Modified 12/10/03
v1= v0 + length(erdata) ;  
s1=( (erdata')*erdata ) +s0; 

if ( floor(v1) - v1 ) == 0 
    nor=randn(v1,1); 
    chi=nor'*nor; 
    sigma=s1/chi; 
else 
    sigma=1./gamrnd(v1/2,2/s1,1,1) 
end 